package com.qinglei.recoup.longconn.utils;

import com.qinglei.recoup.longconn.constant.LongConnConstant;
import com.qinglei.recoup.longconn.entity.BasicBody;
import com.qinglei.recoup.longconn.entity.BodyT;
import com.qinglei.recoup.longconn.entity.DownCtrlBody;
import com.qinglei.recoup.longconn.entity.GrayScaleDataBody;
import com.qinglei.recoup.longconn.entity.GrayScaleDataBodyV1;
import com.qinglei.recoup.longconn.entity.Message;
import com.qinglei.recoup.longconn.entity.UserDataBody;
import com.qinglei.recoup.longconn.entity.UserDataBodyV1;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.BeanUtils;

/**
 * 协议工具类
 */
@Slf4j
public class ProtocolUtils {
    /**
     * 生成心跳响应消息
     *
     * @param msg
     * @return
     */
    public static Message buildHeartBeatReplyMessage(Message msg) {
        Message newMsg = new Message();
        BeanUtils.copyProperties(msg, newMsg);
        newMsg.setFuncCode(LongConnConstant.FUNC_HEART_BEAT_RES);
        return newMsg;
    }

    /**
     * 生成下行消息
     *
     * @param funcCode
     * @param body
     * @return
     */
    public static Message buildDownCtrlMessage(String sn, String funcCode, String body) {
        Message newMsg = new Message();
        newMsg.setHeader(CommonUtils.bytesToHexString(LongConnConstant.PROTOCOL_HEADER));
        newMsg.setProjectCode(LongConnConstant.DEFAULT_PROJECT_CODE);
        newMsg.setFuncCode(funcCode);
        newMsg.setNonce(RandomUtils.nextInt());
        newMsg.setEncMode(LongConnConstant.ENC_MODE_V1);
        newMsg.setSn(sn);
        newMsg.setPn(LongConnConstant.DEFAULT_PN);
        DownCtrlBody downCtrlBody = new DownCtrlBody(body);
        newMsg.setBody(downCtrlBody);
        newMsg.setTail(CommonUtils.bytesToHexString(LongConnConstant.PROTOCOL_TAIL));
        return newMsg;
    }

    /**
     * 解析数据体
     *
     * @param funcCode
     * @param body
     * @param encMode
     * @return BodyT
     */
    public static BodyT decodeBody(String funcCode, byte[] body, Integer encMode) {
        BodyT bodyObj = null;
        if (LongConnConstant.FUNC_UP_USER_DATA.equals(funcCode)) {
            // 上传用户数据
            // 解码用户上传数据
            if (encMode == LongConnConstant.DEFAULT_ENC_MODE) {
                bodyObj = new UserDataBody();
            } else {
                bodyObj = new UserDataBodyV1();
            }
        } else if (LongConnConstant.FUNC_UP_GRAYSCALE_DATA.equals(funcCode)) {
            // 上传灰度数据
            // 解码用户上传数据
            if (encMode == LongConnConstant.DEFAULT_ENC_MODE) {
                bodyObj = new GrayScaleDataBody();
            } else {
                bodyObj = new GrayScaleDataBodyV1();
            }
        } else {
            // 其他指令，解码为基础数据体
            bodyObj = new BasicBody();
        }
        bodyObj.decode(body);
        return bodyObj;
    }

    /**
     * 解码用户上传数据
     * @param body
     * @return
     */
//    public static RadarUserData decodeUserDataBody(byte[] body) {
//        ByteBuf byteBuf = Unpooled.wrappedBuffer(body);
//        RadarUserData radarUserData = new RadarUserData();
//
//        radarUserData.setFrameNo(byteBuf.readInt());
//
//        int[] breathPhaseData = new int[10];
//        for (int i = 0; i <10; i++) {
//            breathPhaseData[i] = byteBuf.readUnsignedShort();
//        }
//        radarUserData.setBreathPhaseData(breathPhaseData);
//
//        int [] heartPhaseData = new int[10];
//        for (int i = 0; i <10; i++) {
//            heartPhaseData[i] = byteBuf.readUnsignedShort();
//        }
//        radarUserData.setHeartPhaseData(heartPhaseData);
//
//        radarUserData.setBreathBpm(byteBuf.readUnsignedByte());
//        radarUserData.setHeartBpm(byteBuf.readUnsignedByte());
//        radarUserData.setS1(byteBuf.readUnsignedByte());
//        radarUserData.setS2(byteBuf.readUnsignedByte());
//        radarUserData.setBreathDepth(byteBuf.readUnsignedByte());
//        radarUserData.setPositionByDoor(byteBuf.readUnsignedByte());
//        radarUserData.setEnergyByDoor(byteBuf.readUnsignedByte());
//        radarUserData.setAbnormal(byteBuf.readUnsignedByte());
//        radarUserData.setOutBedTime(byteBuf.readUnsignedInt());
//        radarUserData.setMaxValDb2(byteBuf.readUnsignedByte());
//        radarUserData.setTabFlag(byteBuf.readUnsignedByte());
//        radarUserData.setRcsMode(byteBuf.readUnsignedByte());
//        radarUserData.setCmBreath(byteBuf.readUnsignedShort());
//        radarUserData.setCmHeart(byteBuf.readUnsignedShort());
//
//        return radarUserData;
//    }

    /**
     * 编码码用户上传数据
     * @param userDataBody
     * @return
     */
//    public static byte[] encodeData(RadarUserData userDataBody) {
//        ByteBuf byteBuf = Unpooled.buffer(DATA_LENGTH);
//
//        int[] breathPhaseData = userDataBody.getBreathPhaseData();
//        for (int i = 0; i <10; i++) {
//            byteBuf.writeShort(breathPhaseData[i]);
//        }
//
//        int [] heartPhaseData = userDataBody.getHeartPhaseData();
//        for (int i = 0; i <10; i++) {
//            byteBuf.writeShort(heartPhaseData[i]);
//        }
//        byteBuf.writeByte(userDataBody.getBreathBpm());
//        byteBuf.writeByte(userDataBody.getHeartBpm());
//        byteBuf.writeByte(userDataBody.getS1());
//        byteBuf.writeByte(userDataBody.getS2());
//        byteBuf.writeByte(userDataBody.getBreathDepth());
//        byteBuf.writeByte(userDataBody.getPositionByDoor());
//        byteBuf.writeByte(userDataBody.getEnergyByDoor());
//        byteBuf.writeByte(userDataBody.getAbnormal());
//        byteBuf.writeInt((int) userDataBody.getOutBedTime());
//        byteBuf.writeByte(userDataBody.getMaxValDb2());
//        byteBuf.writeByte(userDataBody.getTabFlag());
//        byteBuf.writeByte(userDataBody.getRcsMode());
//        byteBuf.writeShort(userDataBody.getCmBreath());
//        byteBuf.writeShort(userDataBody.getCmHeart());
//
//        return byteBuf.array();
//    }
}
